<template>
<div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card>
        <el-row :gutter="20">
            <el-col :span="8">
                <!--搜索框-->
                <el-input placeholder="请输入内容" 
                @keyup.enter.native="getGoodsList"
                v-model="queryInfo.query" clearable class="input-with-select">
                    <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="getAddgoods">添加商品</el-button>
            </el-col>
        </el-row>
        <!--表格区域-->
        <el-table :data="goodsList" border stripe >
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column label="商品名称" prop="goods_name"></el-table-column>
            <el-table-column label="商品价格(元)" width="110" prop="goods_price"></el-table-column>
            <el-table-column label="商品重量"  width="80" prop="goods_weight"></el-table-column>
            <el-table-column label="创建时间" width="160">
                <template slot-scope="scope">
                    {{scope.row.add_time | dateFormat}}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="130px">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini"  icon="el-icon-edit"
                    @click="editGoods(scope.row.goods_id)"></el-button>
                    <el-button type="danger" size="mini" icon="el-icon-delete"
                    @click="deleteGoods(scope.row.goods_id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区 -->
        <Page :total="total" :params="queryInfo" @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"></Page>
    </el-card>
    <router-view></router-view>
</div>
</template>

<script type = "text/javascript">
import Page from '@/components/Page.vue';
export default {
    name: "Li-st",
    components:{Page},
    data(){
        return{
            queryInfo:{
                query:'',
                pagenum:1,
                pagesize:5
            },
            // 商品列表
            goodsList:[],
            // 总数居条数
            total:0,
        }
    },
    watch:{
        "queryInfo.query"(value){
            if (value === "") {
                this.getGoodsList();
            }
        }
    },
    created () {
        this.getGoodsList();
    },
    methods:{
        // 监听change 接收最新页码
        handleCurrentChange(value){
            this.queryInfo.pagenum = value;
            this.getGoodsList();
        },
        // 最新显示条数
        handleSizeChange(value){
            this.queryInfo.pagesize = value;
            this.getGoodsList();
        },
        /**
         * @name getGoodsList
         * @desc 获取商品列表
         */
         getGoodsList: async function (){
            const {data:res} = await this.$http.get('goods',{params:this.queryInfo})
            if (res.meta.status !== 200){
                return this.$message.error('获取商品列表失败');
            }
            this.goodsList = res.data.goods;
            this.total = res.data.total;
         },
        // 添加商品
         getAddgoods(){
            this.$router.push('/goods/add')
         },
        /**
         * @name editGoods
         * @desc 编辑商品
         */
        editGoods: async function (id){
           
            // console.log(res.data);
            this.$router.push({
                path:`/goods/edit/${id}`
            })
        },
        /**
         * @name deleteGoods
         * @desc 删除商品
         */
        deleteGoods:async function (id){
            const confim = await this.$confirm("是否删除该商品？","提示",{
                confirmButtonText:"确定",
                cancelButtonText:"取消",
                type:"warning",
            }).catch(err=>err);
            if (confim !== "confirm") {
               return this.$message.info("取消删除");
            }
            const {data:res} = await this.$http.delete(`goods/${id}`);
            
            if (res.meta.status !==200) {
                return this.$message.error("商品删除失败");
            }
            this.$notify({
                title:"通知",
                type:"success",
                message:"商品删除成功",
            })
            this.getGoodsList();
        }
    }
}
</script>

<style scoped lang = "less">

</style>
